package com.yic.module.device.controller.admin.resume;

import cn.hutool.core.util.ObjectUtil;
import com.yic.module.system.api.user.AdminUserApi;
import com.yic.module.system.api.user.dto.AdminUserRespDTO;
import org.springframework.web.bind.annotation.*;
import javax.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.security.access.prepost.PreAuthorize;
import io.swagger.v3.oas.annotations.tags.Tag;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Operation;

import javax.validation.constraints.*;
import javax.validation.*;
import javax.servlet.http.*;
import java.util.*;
import java.io.IOException;

import com.yic.framework.common.pojo.PageResult;
import com.yic.framework.common.pojo.CommonResult;
import static com.yic.framework.common.pojo.CommonResult.success;

import com.yic.framework.excel.core.util.ExcelUtils;

import com.yic.framework.operatelog.core.annotations.OperateLog;
import static com.yic.framework.operatelog.core.enums.OperateTypeEnum.*;

import com.yic.module.device.controller.admin.resume.vo.*;
import com.yic.module.device.dal.dataobject.resume.ResumeDO;
import com.yic.module.device.convert.resume.ResumeConvert;
import com.yic.module.device.service.resume.ResumeService;

@Tag(name = "管理后台 - 设备履历")
@RestController
@RequestMapping("/device/resume")
@Validated
public class ResumeController {

    @Resource
    private ResumeService resumeService;
    @Resource
    private AdminUserApi adminUserApi;

    @PostMapping("/create")
    @Operation(summary = "创建设备履历")
    @PreAuthorize("@ss.hasPermission('device:resume:create')")
    public CommonResult<Long> createResume(@Valid @RequestBody ResumeCreateReqVO createReqVO) {
        return success(resumeService.createResume(createReqVO));
    }

    @PutMapping("/update")
    @Operation(summary = "更新设备履历")
    @PreAuthorize("@ss.hasPermission('device:resume:update')")
    public CommonResult<Boolean> updateResume(@Valid @RequestBody ResumeUpdateReqVO updateReqVO) {
        resumeService.updateResume(updateReqVO);
        return success(true);
    }

    @DeleteMapping("/delete")
    @Operation(summary = "删除设备履历")
    @Parameter(name = "id", description = "编号", required = true)
    @PreAuthorize("@ss.hasPermission('device:resume:delete')")
    public CommonResult<Boolean> deleteResume(@RequestParam("id") Long id) {
        resumeService.deleteResume(id);
        return success(true);
    }

    @GetMapping("/get")
    @Operation(summary = "获得设备履历")
    @Parameter(name = "id", description = "编号", required = true, example = "1024")
    @PreAuthorize("@ss.hasPermission('device:resume:query')")
    public CommonResult<ResumeRespVO> getResume(@RequestParam("id") Long id) {
        ResumeDO resume = resumeService.getResume(id);
        return success(ResumeConvert.INSTANCE.convert(resume));
    }

    @GetMapping("/list")
    @Operation(summary = "获得设备履历列表")
    @Parameter(name = "ids", description = "编号列表", required = true, example = "1024,2048")
    @PreAuthorize("@ss.hasPermission('device:resume:query')")
    public CommonResult<List<ResumeRespVO>> getResumeList(@RequestParam("ids") Collection<Long> ids) {
        List<ResumeDO> list = resumeService.getResumeList(ids);
        return success(ResumeConvert.INSTANCE.convertList(list));
    }

    @GetMapping("/page")
    @Operation(summary = "获得设备履历分页")
    @PreAuthorize("@ss.hasPermission('device:resume:query')")
    public CommonResult<PageResult<ResumeRespVO>> getResumePage(@Valid ResumePageReqVO pageVO) {
        PageResult<ResumeDO> pageResult = resumeService.getResumePage(pageVO);
        PageResult<ResumeRespVO> result = ResumeConvert.INSTANCE.convertPage(pageResult);
        result.getList().forEach(item->{
            // 封装操作人
            AdminUserRespDTO user = adminUserApi.getUser(item.getUserId());
            if (ObjectUtil.isNotEmpty(user)) {
                item.setUserName(user.getNickname());
            }
        });
        return success(result);
    }

    @GetMapping("/export-excel")
    @Operation(summary = "导出设备履历 Excel")
    @PreAuthorize("@ss.hasPermission('device:resume:export')")
    @OperateLog(type = EXPORT)
    public void exportResumeExcel(@Valid ResumeExportReqVO exportReqVO,
              HttpServletResponse response) throws IOException {
        List<ResumeDO> list = resumeService.getResumeList(exportReqVO);
        // 导出 Excel
        List<ResumeExcelVO> datas = ResumeConvert.INSTANCE.convertList02(list);
        ExcelUtils.write(response, "设备履历.xls", "数据", ResumeExcelVO.class, datas);
    }

}
